﻿using System.Threading.Tasks;
using System.Web.Mvc;
using Abp.Web.Mvc.Authorization;
using KDS.Mes.Authorization;
using KDS.Mes.Users;
using Abp.Web.Models;
using KDS.Mes.Authorization.Users.Dto;
using Abp.Application.Services.Dto;
using KDS.Mes.Web.Models.Users;

namespace KDS.Mes.Web.Controllers
{
    [AbpMvcAuthorize(AppPermissions.Pages_Administration_Users)]
    public class UsersController : MesControllerBase
    {
        private readonly IUserAppService _userAppService;

        public UsersController(IUserAppService userAppService)
        {
            _userAppService = userAppService;
        }

        //public async Task<ActionResult> Index()
        //{
        //    var output = await _userAppService.GetUsers();
        //    return View(output);
        //}
        public  ActionResult Index()
        {
            return View();
        }

        [DontWrapResult]
        public JsonResult GetAllUsers(int limit, int offset, string sortfiled, string sortway)
        {
            var sort = !string.IsNullOrEmpty(sortfiled) ? string.Format("{0} {1}", sortfiled, sortway) : "";
            var filter = new GetUsersInput
            {
                SkipCount = offset,
                MaxResultCount = limit,
                Sorting = sort,
            };
            var pagedTasks = _userAppService.GetPagedUsers(filter);
            return Json(new { total = pagedTasks.TotalCount, rows = pagedTasks.Items }, JsonRequestBehavior.AllowGet);
        }

        [AbpMvcAuthorize(AppPermissions.Pages_Administration_Users_Create, AppPermissions.Pages_Administration_Users_Edit)]
        public async Task<PartialViewResult> CreateOrEditModal(long? id)
        {
            var output = await _userAppService.GetUserForEdit(new NullableIdDto<long> { Id = id });
            var viewModel = new CreateOrEditUserModalViewModel(output);

            return PartialView("_CreateOrEditModal", viewModel);
        }
    }
}